package com.woniu.config;

import com.woniu.utils.ConstantUtil;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    //延迟取消订单 延迟交换机
    @Bean
    public CustomExchange checkOrderExchange(){
        Map<String, Object> map = new HashMap<>();
        map.put("x-delayed-type","topic");
        return new CustomExchange(
                ConstantUtil.EXCHANGE_CHECK_ORDER_STATUS,
                "x-delayed-message",
                true,
                false,
                map);
    }
    // 延迟队列
    @Bean
    public Queue delayQueue(){
        return new Queue(ConstantUtil.DELAY_QUEUE);
    }
    // 绑定关系
    @Bean
    public Binding delayQueueToExchange(){
        return BindingBuilder.bind(delayQueue()).to(checkOrderExchange())
                .with(ConstantUtil.ROUTING_KEY_DELAY).noargs();
    }
    //异步生成顶单 topic交换机
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(ConstantUtil.EXCHANGE_KILL_ORDER);
    }
    // topic队列
    @Bean
    public Queue topicQueue(){
        return new Queue(ConstantUtil.QUEUE_KILL_ORDER);
    }   @Bean
    public Queue topicQueue2(){
        return new Queue(ConstantUtil.QUEUE_ORDER);
    }
    // 绑定关系
    @Bean
    public Binding topicQueueToExchange(){
        return BindingBuilder.bind(topicQueue()).to(topicExchange()).with(ConstantUtil.ROUTING_KEY_KILL_ORDER);
    }@Bean
    public Binding topicQueueToExchange2(){
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with(ConstantUtil.ROUTING_KEY_ORDER);
    }
}
